package com.davinci.freemarket.businessLayer.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.davinci.freemarket.businessLayer.controllers.PublicationsController;
import com.davinci.freemarket.dataModelLayer.model.PublicationModel;

public class BrowserServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {

		String search = request.getParameter("search");
		PrintWriter pw;
		try {
			pw = response.getWriter();

			PublicationsController publicationsController = new PublicationsController();

			if (search.length() < 1 || search == null)
				search = "";
			
			ArrayList<PublicationModel> publications = publicationsController.searchPublications(search);
			
			if (publications.size() > 0) {
				HttpSession session = request.getSession(true);
				session.setAttribute("publications", publications);
				response.sendRedirect("../publicationViews.jsp");
			} else {
				pw.println("la lista es 0 o null");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doPost(req, resp);
	}

}
